/*

  Cone search

  Copyright © 2010 - 2011 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "munipack.h"
#include <wx/wx.h>
#include <iostream>
#include <map>

using namespace std;


bool Munipack::cone(MuniCmdLineParser& cmd)
{
  map<wxString,wxString> catalogs;
  catalogs["Simbad"] = "http://simbad.u-strasbg.fr/simbad/sim-cone?";
  catalogs["UCAC3"] = "http://vizier.u-strasbg.fr/viz-bin/votable/-A?-source=I/315&";

  /* BAD practise - the source of catalogue is dubled 
      in xmunipack/astrometry.cpp
  */

  if( cmd.Found("list") ) {
    cout << "Available catalogues  (use --url to specify another):" << endl;
    cout << " Alias  URL --------------------------" << endl;
    for(map<wxString,wxString>::const_iterator i = catalogs.begin() ; i != catalogs.end(); i++ )
      cout << (*i).first << "  " << (*i).second << endl;
    return false;
  }

  if ( cmd.GetParamCount() < 2 )
    wxLogFatalError("Unspecified position? Try: RA,DEC (both in degress, sexadecimal).");

  MuniProcess *c = new MuniProcess(&pipe,"cone");
  pipe.push(c);

  wxString a;

  if( cmd.Found("r",&a) )
    c << ("SR = '" + a + "'");

  if( cmd.Found("par",&a) )
    c << ("PAR = '" + a + "'");

  if( cmd.Found("o",&a) )
    c << ("OUTPUT = '" + a + "'");

  if( cmd.Found("t",&a) )
    c << ("TYPE = '" + a.MakeUpper() + "'");

  if( cmd.Found("s",&a) )
    c << ("SORT = '" + a + "'");

  if( cmd.Found("cat",&a) ) {
    map<wxString,wxString>::const_iterator i = catalogs.find(a);
    if( i != catalogs.end() )
      c << ("URL = '" + i->second + "'");
    else
      wxLogFatalError("Required catalogue not available (try --list or --url).");      
  }
  else {
    if( cmd.Found("url",&a) )
      c << ("URL = '" + a + "'");
    else
      c << ("URL = '" + catalogs.begin()->second + "'");
  }


  for(size_t i = 1; i < cmd.GetParamCount(); i++) {
    wxString p(cmd.GetParam(i));
    int pos = p.Find(',');
    if( pos != wxNOT_FOUND ) {
      c << ("RA = '" + p.substr(0,pos) + "'");
      c << ("DEC = '" + p.substr(pos+1) + "'");
    }
  }

  pipe.Start();

  return true;
}
